package com.yanqu.road.entity.activity.sonhai.config;

import com.yanqu.road.entity.activity.sonhai.enums.SonHaiPlotTypeEnum;
import com.yanqu.road.entity.activity.sonhai.enums.SonHaiYNEnum;
import com.yanqu.road.utils.RandomHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

public class SonHaiEventConfig {

    /**
     * 活动id
     */
    private int activityId;

    /**
     * 事件ID
     */
    private int id;

    /**
     * 图标id
     */
    private int icon;

    /**
     * 事件类型
     */
    private int eventType;

    /**
     * 事件名称
     */
    private String name;

    /**
     * 怪
     */
    private String monster;
    private List<Integer> monsterList = new ArrayList<>();

    /**
     * 地块描述
     */
    private String eventDesc;

    /**
     * 事件描述
     */
    private String desc;

    /**
     * 简略描述
     */
    private String simpleDesc;

    /**
     * 等级
     */
    private int level;

    /**
     * 事件参数
     */
    private String param;
    private List<Integer> paramList = new ArrayList<>();

    /**
     * 道具奖励(｜区分池子＃区分每个池子里不同的道具组合的权重表示一个道具组合里有多少种道具)
     */
    private String reward;

    /**
     * 寻踪进度
     */
    private int schedule;

    /**
     * 伏兽积分
     */
    private long monsterScore;

    /**
     * 山海灵气
     */
    private long pvpScore;

    /**
     * 二选一事件奖励（正确奖励|错误奖励）道具id=道具数量；道具id=道具数量
     */
    private String chooseReward;
    private List<String> chooseRewardList = new ArrayList<>();

    public void setActivityId(int activityId) {
        this.activityId = activityId;
    }

    public int getActivityId() {
        return activityId;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getId() {
        return id;
    }

    public void setIcon(int icon) {
        this.icon = icon;
    }

    public int getIcon() {
        return icon;
    }

    public void setEventType(int eventType) {
        this.eventType = eventType;
    }

    public int getEventType() {
        return eventType;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public String getDesc() {
        return desc;
    }

    public String getSimpleDesc() {
        return simpleDesc;
    }

    public void setSimpleDesc(String simpleDesc) {
        this.simpleDesc = simpleDesc;
    }

    public void setLevel(int level) {
        this.level = level;
    }

    public int getLevel() {
        return level;
    }

    public void setParam(String param) {
        this.param = param;
        if (param != null && !param.isEmpty()) {
            String[] params = param.split("\\|");
            List<Integer> paramList = new ArrayList<>();
            for (String item : params) {
                paramList.add(Integer.parseInt(item));
            }
            this.paramList = paramList;
        }
    }

    public String getParam() {
        return param;
    }

    public List<Integer> getParamList() {
        return paramList;
    }

    public void setReward(String reward) {
        this.reward = reward;
    }

    public String getReward() {
        return reward;
    }

    public void setSchedule(int schedule) {
        this.schedule = schedule;
    }

    public int getSchedule() {
        return schedule;
    }

    public void setMonsterScore(long monsterScore) {
        this.monsterScore = monsterScore;
    }

    public long getMonsterScore() {
        return monsterScore;
    }

    public void setPvpScore(long pvpScore) {
        this.pvpScore = pvpScore;
    }

    public long getPvpScore() {
        return pvpScore;
    }

    public String getMonster() {
        return monster;
    }

    public void setMonster(String monster) {
        this.monster = monster;
        if (!StringUtils.isNullOrEmpty(monster)) {
            String[] monsters = monster.split("\\|");
            List<Integer> monsterList = new ArrayList<>();
            for (String item : monsters) {
                monsterList.add(Integer.parseInt(item));
            }
            this.monsterList = monsterList;
        }
    }

    public String getEventDesc() {
        return eventDesc;
    }

    public void setEventDesc(String eventDesc) {
        this.eventDesc = eventDesc;
    }

    public List<Integer> getMonsterList() {
        return monsterList;
    }

    public String getChooseReward() {
        return chooseReward;
    }

    public void setChooseReward(String chooseReward) {
        this.chooseReward = chooseReward;
        if (!StringUtils.isNullOrEmpty(chooseReward) && !chooseReward.equals("0")) {
            String[] rewards = chooseReward.split("\\|");
            this.chooseRewardList = new ArrayList<>(Arrays.asList(rewards));
        }
    }

    public List<String> getChooseRewardList() {
        return chooseRewardList;
    }

    /**
     * 获取2选1奖励
     * @param rightOrWrong
     * @return
     */
    public String getChooseReward(int rightOrWrong) {
        if (rightOrWrong > 0) {
            return chooseRewardList.get(0);
        } else {
            return chooseRewardList.get(1);
        }
    }

    /**
     * 随机出异兽
     * @return
     */
    public int randomMonsterId() {
        if (this.monsterList != null && !this.monsterList.isEmpty()) {
            Random random = new Random();
            return this.monsterList.get(random.nextInt(this.monsterList.size()));
        }
        return 0;
    }

    /**
     * 获取正确的选项下标
     * @return
     */
    public int getChooseTrueIndex() {
        if (this.eventType != SonHaiPlotTypeEnum.PLOT_TYPE_16.getType()) {
            return 0;
        }
        String[] descStrArr =this.desc.split("\\|");
        if (descStrArr.length < 4) {
            return 0;
        }
        //选项1
        String chooseStr1 = descStrArr[2];
        boolean isTrue1 = this.checkChooseTrueIndex(chooseStr1);
        if (isTrue1) {
            return 0;//0，标识第一个选项是正确答案
        }
        //选项1
        String chooseStr2 = descStrArr[3];
        boolean isTrue2 = this.checkChooseTrueIndex(chooseStr2);
        if (isTrue2) {
            return 1;//1，标识第二个选项是正确答案
        }
        return 0;
    }

    /**
     * 判断是否是正确选项
     * @param chooseStr
     * @return
     */
    private boolean checkChooseTrueIndex(String chooseStr) {
        String[] chooseStr1StrArr = chooseStr.split(";");
        if (chooseStr1StrArr.length >= 3) {
            int trueFlag = Integer.parseInt(chooseStr1StrArr[2]);
            if (trueFlag == SonHaiYNEnum.YES.getType()) {
                return true;
            }
        }
        return false;
    }
}
